package com.woniu.service.impl;

import com.woniu.entity.RbacManager;
import com.woniu.entity.ResponseEntity;
import com.woniu.mapper.LoginMapper;
import com.woniu.service.LoginService;
import com.woniu.utils.JWTUtils;
import com.woniu.utils.SecurityCryUtil;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginException;
import java.util.Objects;

/**
 * @author Rain
 * @Description :
 * @date 2022/09/05 17:24
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    LoginMapper mapper;

    @Override
    public ResponseEntity login(String username, String password) throws LoginException {
        String newPassword = SecurityCryUtil.generateEncryptedPassword(password, username);

        RbacManager rbacManager = mapper.findOne(username, newPassword);
        if (Objects.nonNull(rbacManager)) {
            ResponseEntity responseEntity = new ResponseEntity(200,"登陆成功",rbacManager);
            String token = JWTUtils.sign(rbacManager.getAccount(),rbacManager.getPassword());
            responseEntity.put("token",token);
            return responseEntity;
        }else {
            throw new LoginException("账户名或密码错误");
        }
    }
}
